define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/dom-construct',
      'dijit/form/CheckBox'
   ],
   function(declare, lang, domConstruct, CheckBox)
   {
      //mSelectionStore
      return function(column)
      {
         var originalRenderCell = column.renderCell || function(pItem, value, td){
            
         };

         column.renderCell = function(pItem, value, td, options){

            var pChk = new CheckBox({
               name:'checkbox',
               value:'selected',
               checked:false,
               style: 'position:relative; bottom: 4px;',
               onClick:lang.hitch(this, function(){
                  if(!this.grid.mSelectionStore)
                     return;
                  
                  if(pChk.get('value') == 'selected')
                     this.grid.mSelectionStore.put(pItem);
                  else
                     this.grid.mSelectionStore.remove(pItem[this.grid.mSelectionStore.idProperty]);
               })
            });
            
            var pItemInStore = this.grid.mSelectionStore && this.grid.mSelectionStore.get(pItem[this.grid.mSelectionStore.idProperty]);
            if(pItemInStore)
               pChk.set('checked', true);

            if(this.grid.checkboxEnabled)
               pChk.set('readOnly', !this.grid.checkboxEnabled(pItem, pItemInStore));
            
            if(this.grid.selectionCheckboxValue)
            {
               var returnedValue = this.grid.selectionCheckboxValue(pItem, pItemInStore);
               if(!returnedValue)
               {
                  pChk.set('checked', false);
               }
               else if(returnedValue === true)
               {
                  pChk.set('checked', true);
                  if(!pItemInStore && this.grid.mSelectionStore)
                     this.grid.mSelectionStore.put(pItem);
               }
               else
               {
                  // Is deffered
                  returnedValue.then(lang.hitch(this, function(pResultObject){
                     pChk.set('checked', pResultObject.value);
                     if(pResultObject.value)
                     {
                        if(!pItemInStore && this.grid.mSelectionStore)
                           this.grid.mSelectionStore.put(pItem);
                     }
                     else
                     {
                        if(pItemInStore && this.grid.mSelectionStore)
                           this.grid.mSelectionStore.remove(pItem);
                     }
                  }));
               }
            }

            domConstruct.place(pChk.domNode, td, 'only');

            originalRenderCell.call(this, pItem, value, td, options);
         };

         return column;
      };
   }
);

